#include "clickedlabel.h"
#include<QMouseEvent>
#include<QDebug>

ClickedLabel::ClickedLabel(QWidget *parent): QLabel(parent), _cur_state(ClickLbState::Normal)
{

}

void ClickedLabel::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
           if(_cur_state == ClickLbState::Normal){
                 qDebug()<<"clicked , change to selected hover: "<< _selected_hover;
               _cur_state = ClickLbState::Selected;
               setProperty("state",_selected_hover);
               repolish(this);
               update();

           }else{
                  qDebug()<<"clicked , change to normal hover: "<< _normal_hover;
               _cur_state = ClickLbState::Normal;
               setProperty("state",_normal_hover);
               repolish(this);
               update();
           }
           emit clicked();
   }
   // 调用基类的mousePressEvent以保证正常的事件处理
   QLabel::mousePressEvent(event);
}

void ClickedLabel::enterEvent(QEvent *event)
{
    // 在这里处理鼠标悬停进入的逻辑
      if(_cur_state == ClickLbState::Normal){
           qDebug()<<"enter , change to normal hover: "<< _normal_hover;
          setProperty("state",_normal_hover);
          repolish(this);
          update();

      }else{
           qDebug()<<"enter , change to selected hover: "<< _selected_hover;
          setProperty("state",_selected_hover);
          repolish(this);
          update();
      }

      QLabel::enterEvent(event);
}

void ClickedLabel::leaveEvent(QEvent *event)
{
    // 在这里处理鼠标悬停离开的逻辑
       if(_cur_state == ClickLbState::Normal){
            qDebug()<<"leave , change to normal : "<< _normal;
           setProperty("state",_normal);
           repolish(this);
           update();

       }else{
            qDebug()<<"leave , change to normal hover: "<< _selected;
           setProperty("state",_selected);
           repolish(this);
           update();
       }
       QLabel::leaveEvent(event);
}

void ClickedLabel::SetState(QString normal, QString normal_hover, QString normal_press, QString selected, QString selected_hover, QString selected_press)
{
        _normal = normal;
       _normal_hover = normal_hover;
       _normal_press = normal_press;

       _selected = selected;
       _selected_hover = selected_hover;
       _selected_press = selected_press;

       setProperty("state",normal);
       repolish(this);
}

ClickLbState ClickedLabel::GetCurState()
{
    return _cur_state;
}
